package com.example.bysj_sb.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.bysj_sb.domain.GraduateProject;
import com.example.bysj_sb.service.GraduateProjectCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class GraduateProjectCacheController {
    //生成一个实例
    @Autowired
    private GraduateProjectCacheService graduateProjectCacheService;
    @GetMapping("/cache.ctl/{teacher_id}")
    public JSONObject findByTeacherId(@PathVariable("teacher_id") int teacher_id){
       List<GraduateProject> graduateProjects = graduateProjectCacheService.findByTeacherId(teacher_id);
       JSONObject message = new JSONObject();
       message.put("我的课题",graduateProjects);
       return message;
    }
    @PostMapping("/cache.ctl")
    public String add(@RequestBody GraduateProject graduateProject){
        Boolean added =false;
        graduateProject.getGraduateProjectStatus().setId(2);
        graduateProject.getGraduateProjectStatus().setDescription("暂存");
        added= graduateProjectCacheService.add(graduateProject);
        if (added){
            return "增加成功！";
        }else {
            return "增加失败！";
        }
    }
    @DeleteMapping("/cache.ctl/{id}")
    public String delete(@PathVariable("id") int id){
        Boolean deleted =false;
        deleted= graduateProjectCacheService.delete(id);
        if (deleted){
            return "删除成功！";
        }else {
            return "删除失败！";
        }
    }
    @PutMapping("/cache.ctl/{id}")
    public  String update(@PathVariable("id") int id,@RequestBody GraduateProject graduateProject){
        Boolean updated =false;
        graduateProject.setId(id);
        updated= graduateProjectCacheService.update(graduateProject);
        if (updated){
            return "修改成功！";
        }else {
            return "修改失败！";
        }
    }
}
